﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcWeb.Models;
using SailHero.Soft.FrameWork.IDomainRepositories.Contracts;
using SailHero.Soft.FrameWork.Models;

namespace MvcWeb.Controllers
{
    [Export(typeof(IController)), ExportMetadata("Name", "Dept")]
    public class DeptController : BaseController
    {
        /// <summary>
        /// 数据操作接口
        /// </summary>
        [Import]
        public IDeptRepository deptModel;
        [Import]
        public ISys_UserRepository userModel;
        [Authorize]
        public ActionResult Index(string page)
        {
            var list = deptModel.GetAllList();
            list = list.OrderBy(d => d.ORDERNUM);
            int size = 10;
            if (page != null)
            {
                ViewData["page"] = PagerHelper.Pager(Request.RawUrl.Substring(0, Request.RawUrl.IndexOf("?")), Convert.ToInt32(page), list.Count(), size, 4);
                list = list.Skip((Convert.ToInt32(Request["page"]) - 1) * size).Take(size);
            }
            else
            {
                ViewData["page"] = PagerHelper.Pager(Request.RawUrl, 1, list.Count(), size, 4);
                list = list.Skip((1 - 1) * size).Take(size);
            }
            return View(list.AsEnumerable());
        }

        [Authorize]
        public ActionResult Create()
        {
            ViewData["deptList"] = new SelectList(deptModel.GetAllList(), "deptID", "DeptCName");
            ViewData["userList"] = new SelectList(userModel.GetAllList(), "UserID", "UserCName");
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                SYS_DEPARTMENT mod = new SYS_DEPARTMENT();
                mod.DEPTID = DateTime.Now.Ticks.ToString();
                mod.DEPTCNAME = Request.Form["DeptCName"];
                mod.DEPARTMANAGER = Request.Form["userList"];
                mod.TELEPHONE = Request.Form["Telephone"];
                mod.FAX = Request.Form["Fax"];
                mod.TELEPHONE = Request.Form["Telephone"];
                mod.PARTENTID = Request.Form["deptList"];
                mod.ORDERNUM = string.IsNullOrEmpty(Request.Form["OrderNum"]) ? 999 : Convert.ToInt32(Request.Form["OrderNum"]);
                mod.ISENABLE = Request.Form["isEnable"] == "on" ? "1" : "0";
                mod.CREATOR = LoginUser.ACCOUNT;
                mod.CREATEDATE = DateTime.Now;

                deptModel.Add(mod);
                deptModel.UnitOfWork.SaveChanges();
                return RedirectToAction("Index");

            }
            catch (Exception ee)
            {
                ViewData["deptList"] = new SelectList(deptModel.GetAllList(), "deptID", "DeptCName");
                ViewData["userList"] = new SelectList(userModel.GetAllList(), "UserID", "UserCName");
                return View();
            }
        }

        [Authorize]
        public ActionResult Edit(string id)
        {
            SYS_DEPARTMENT mod = deptModel.FirstOrDefault(itm => itm.DEPTID == id);
            ViewData["deptList"] = new SelectList(deptModel.GetAllList(), "deptID", "DeptCName", mod.PARTENTID);
            ViewData["userList"] = new SelectList(userModel.GetAllList(), "UserID", "UserCName", mod.DEPARTMANAGER);
            return View(mod);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Edit(string DeptID, FormCollection collection)
        {
            SYS_DEPARTMENT mod = deptModel.Get(DeptID);
            try
            {
                mod.DEPTCNAME = Request.Form["DeptCName"];
                mod.DEPARTMANAGER = Request.Form["userList"];
                mod.TELEPHONE = Request.Form["Telephone"];
                mod.FAX = Request.Form["Fax"];
                mod.PARTENTID = Request.Form["deptList"];
                mod.ORDERNUM = string.IsNullOrEmpty(Request.Form["OrderNum"]) ? 999 : Convert.ToInt32(Request.Form["OrderNum"]);
                mod.ISENABLE = Request.Form["isEnable"] == "false" ? "1" : "0";
                mod.UPDATEUSER = LoginUser.ACCOUNT;
                mod.UPDATEDATE = DateTime.Now;

                deptModel.UnitOfWork.SaveChanges();
                return RedirectToAction("Index");
               

            }
            catch (Exception ee)
            {
                ViewData["deptList"] = new SelectList(deptModel.GetAllList(), "deptID", "DeptCName", mod.PARTENTID);
                ViewData["userList"] = new SelectList(userModel.GetAllList(), "UserID", "UserCName", mod.DEPARTMANAGER);
                return View(mod);
            }
        }

        [Authorize]
        [HttpPost]
        public string Delete(string deptID)
        {
            try
            {
                // TODO: Add delete logic here
                deptModel.Delete(itm => itm.DEPTID == deptID);
                deptModel.UnitOfWork.SaveChanges();
                return "success";
            }
            catch
            {
                return "fail";
            }
        }
    }
}
